import { Window_Selectable } from "@/windows/Window_Selectable.js";
import { round } from "@/utils/number.js";

export class Window_CommandList extends Window_Selectable{
    constructor(width,height){
        super(width,height,false);
        this.init(width,height);
    }
    init(width,height){
        /**
         * @type { {
         * name,
         * symbol,
         * enabled,
         * ext
         * }[] }
         */
        this._list=[];
        this._handlers={};

        this.okHandler=null;
        this.cancelHandler=null;

        this.batch=false;

        this.makeCommandList();
        super.init(width,round(height,this.itemHeight()));
    }

    destroy(opts){
        super.destroy(opts);
        this._handlers={};
        this.okHandler=null;
        this.cancelHandler=null;
    }

    row(){return this.length();}
    viewRow(){return Math.floor(this.contentsHeight()/this.itemHeight());}
    length(){return this._list.length;}
    
    itemEnabled(index){
        return super.itemEnabled(index)&&this._list[index]?.enabled;
    }

    drawItem(index,x,y,width,height){
        let item=this._list[index];
        if(item){
            let enabled=this.itemEnabled(index);
            if(!enabled){
                this.changePaintOpacity(false);
            }
            this.drawText(item.name,x,y,width,height,'center');
            if(!enabled){
                this.changePaintOpacity(true);
            }
        }
    }

    addCommand(name,symbol,ext,enabled=true){
        let item=null;
        for(let i=0;i<this._list.length;++i){
            item=this._list[i];
            if(item.name==name&&item.symbol==symbol){
                if(item.enabled!=enabled){
                    item.enabled=enabled;
                    if(!this.batch){
                        this.refreshItem(i);
                    }
                }
                return;
            }
        }
        this._list.push({name,symbol,enabled,ext});
        if(!this.batch){
            this.refreshItem(this.length()-1);
        }
    }

    addCommands(...args){
        this.batch=true;
        for(let item of args){
            this.addCommand(...item);
        }
        this.batch=false;

        this.refresh();
    }

    removeCommand(name,symbol){
        if(name&&!symbol){
            let i=0;
            while(i<this._list.length){
                if(this._list[i].name==name){
                    this._list.splice(i,1);
                }else{
                    ++i;
                }
            }
        }else if(!name&&symbol){
            let i=0;
            while(i<this._list.length){
                if(this._list[i].symbol==symbol){
                    this._list.splice(i,1);
                }else{
                    ++i;
                }
            }
        }else if(name&&symbol){
            for(let i=0;i<this._list.length;++i){
                let item=this._list[i];
                if(item.name==name&&item.symbol==symbol){
                    this._list.splice(i,1);
                    break;
                }
            }
        }else return;

        if(!this.batch){
            this.refresh();
        }
    }

    removeCommands(...args){
        this.batch=true;
        for(let item of args){
            this.removeCommand(...item);
        }
        this.batch=false;
        this.refresh();
    }

    makeCommandList(){
        this.batch=true;
        this.makeCommands();
        this.batch=false;
    }
    makeCommands(){}

    setHandler(symbol,callback){
        if(symbol=='ok'){
            this.okHandler=callback;
        }else if(symbol=='cancel'){
            this.cancelHandler=callback;
        }else{
            this._handlers[symbol]=callback;
        }
    }

    onResponse(index){
        let item=this._list[index];
        if(item){
            let callback=this._handlers[item.symbol]??this.okHandler;
            callback?.(item,index);
        }
    }

    onCancel(){
        this.cancelHandler?.();
    }
}